<template>
  <div>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="10">
        <el-col :span="3">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加教学周期</el-button
          >
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="teachingCyclelist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="教学周期标题" prop="title"></el-table-column>
        <el-table-column
          label="开始日期"
          prop="starttime"
        >
        <template slot-scope="scope">
          {{scope.row.starttime | datetime}}
        </template>
        </el-table-column>
        <el-table-column
          label="结束日期"
          prop="endtime"
        >
        <template slot-scope="scope">
          {{scope.row.endtime | datetime}}
        </template>
        </el-table-column>
        <el-table-column label="是否开始" prop="is_start">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_start"
              @change="isStartChanged(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeTimes(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改用户的对话框 -->
    <el-dialog
      title="修改教学周期"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
    <el-form
        :model="editForm"
        :rules="FormRules"
        ref="editFormRef"
        label-width="110px"
      >
        <el-form-item label="教学周期标题" prop="title">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item label="开始日期" prop="starttime">
          <el-date-picker
            @change="selectDateChange()"
            v-model="editForm.starttime"
            type="datetime"
            placeholder="选择开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endtime">
          <el-date-picker
            @change="selectDateChange()"
            v-model="editForm.endtime"
            type="datetime"
            placeholder="选择结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="休息日期" prop="no_class_date">
          <el-select
            filterable
            multiple
            v-model="editForm.no_class_date"
            placeholder="请选择休息日期"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上午上课日期" prop="morning_class_date">
          <el-select
            filterable
            multiple
            v-model="editForm.morning_class_date"
            placeholder="请选择上午上课日期"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下午上课日期" prop="afternoon_class_date">
          <el-select
            filterable
            multiple
            v-model="editForm.afternoon_class_date"
            placeholder="请选择下午上课日期"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDepartment">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加教学周期的对话框 -->
    <el-dialog
      title="添加教学周期"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="FormRules"
        ref="addFormRef"
        label-width="110px"
      >
        <el-form-item label="教学周期标题" prop="title">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="开始日期" prop="starttime">
          <el-date-picker
            @change="selectDateChange()"
            v-model="addForm.starttime"
            type="datetime"
            placeholder="选择开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endtime">
          <el-date-picker
            @change="selectDateChange()"
            v-model="addForm.endtime"
            type="datetime"
            placeholder="选择结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="休息日期" prop="no_class_date">
          <el-select
            filterable
            multiple
            v-model="addForm.no_class_date"
            placeholder="请选择休息日期"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上午上课日期" prop="morning_class_date">
          <el-select
            filterable
            multiple
            v-model="addForm.morning_class_date"
            placeholder="请选择上午上课日期"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下午上课日期" prop="afternoon_class_date">
          <el-select
            filterable
            multiple
            v-model="addForm.afternoon_class_date"
            placeholder="请选择下午上课日期"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTeachingTime">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addTeachingTime,
  getTeachingTime,
  editTeachingTime,
  removeTeachingTime,
  getTeachingTimes
} from '../../api/teachingtime'
import moment from 'moment'
export default {
  data() {
    return {
      queryInfo: {
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5,
      },
      teachingCyclelist: [],
      total: 0,
      dateOptions: [],
      addForm: {
        title: '',
        starttime: '',
        endtime: '',
        no_class_date: [],
        morning_class_date: [],
        afternoon_class_date: [],
      },
      FormRules: {
        title: [
          { required: true, message: '请输入教学周期名称', trigger: 'blur' },
          {
            max: 30,
            message: '职位名称的长度在30个字符之内',
            trigger: 'blur',
          },
        ],
        starttime: [
          { required: true, message: '请选择教学周期开始时间', trigger: 'blur' },
        ],
        endtime: [
          { required: true, message: '请选择教学周期结束时间', trigger: 'blur' },
        ],
      },
      
      // 查询到的信息对象
      editForm: {
        
      },

      
    }
  },
  created() {
    this.getTeachingCycleList()
  },
  methods: {
    //获取教学周期列表
    async getTeachingCycleList(){
        let {count,data}=await getTeachingTime(this.queryInfo)
        this.teachingCyclelist=data
        this.total=count
        // console.log(this.count)
        // console.log(this.teachingCyclelist)

    },
    //修改教学周期状态
    async isStartChanged(row){
      if(await editTeachingTime(row)){
        this.$message.success('教学周期状态更新成功')
        this.teachingCyclelist.forEach(item=>{
          if(item.id!=row.id){
            item.is_start=false
          }
        })
      }
    },
    selectDateChange() {
      let startDate = moment(this.addForm.starttime)
      let endDate = moment(this.addForm.endtime)
      this.dateOptions=[]
      if (endDate && startDate) {
        while (endDate >= startDate) {
          this.dateOptions.push(startDate.format('YYYY-MM-DD'))
          startDate.add(1, 'day')
        }
      }
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getTeachingCycleList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getTeachingCycleList()
    },

    addTeachingTime() {
      // console.log(this.addForm)
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return
        this.addForm.starttime=moment(this.addForm.starttime).format('YYYY-MM-DD HH:mm:ss')
        this.addForm.endtime=moment(this.addForm.endtime).format('YYYY-MM-DD HH:mm:ss')
        //可以发起添加用户的网络请求
        if (await addTeachingTime(this.addForm)) {
          this.addDialogClosed()
          this.$message.success('成功添加教学周期！')
          this.getTeachingCycleList()
        }
      })
    },

    // 展示编辑的对话框
    async showEditDialog(i) {
      this.editDialogVisible = true
      // console.log(i) 
      let res = await getTeachingTimes(i)
      console.log(res)
      this.editForm=res
      this.editForm = res
      this.editForm.no_class_date = JSON.parse(this.editForm.no_class_date)
      this.editForm.morning_class_date = JSON.parse(this.editForm.morning_class_date)
      this.editForm.afternoon_class_date = JSON.parse(this.editForm.afternoon_class_date)

      let startDate = moment(res.starttime)
      let endDate = moment(res.endtime)
      this.dateOptions = []
      if (endDate && startDate) {
        while (endDate >= startDate) {
          this.dateOptions.push(startDate.format('YYYY-MM-DD'))
          startDate.add(1, 'day')
        }
      }
      
    },

    // 修改部门信息并提交
    editDepartment() {
      console.log(this.editForm)  
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        this.editForm.starttime = moment(this.editForm.starttime).format("YYYY-MM-DD HH:mm:ss")
        this.editForm.endtime = moment(this.editForm.endtime).format("YYYY-MM-DD HH:mm:ss")
        this.editForm.no_class_date = JSON.stringify(this.editForm.no_class_date)
        this.editForm.morning_class_date = JSON.stringify(this.editForm.morning_class_date)
        this.editForm.afternoon_class_date = JSON.stringify(this.editForm.afternoon_class_date)
        const res = await editTeachingTime(this.editForm)
        this.editDialogClosed()
        this.$message.success('成功更新教学周期！')
        this.getTeachingCycleList()
      
        
      })
      // this.editDialogVisible =! this.editDialogVisible
    },
    // 根据Id删除对应的用户信息
    async removeTimes(id) {
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        '此操作将永久删除该职位, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      removeTeachingTime(id).then((value) => {
        this.$message.success('删除部门成功！')
        this.getTeachingCycleList()
      })
    },
  },
}
</script>

<style scoped>

.el-date-editor,.el-select{
  width: 100%;
}
</style>
